/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao.blog.service.impl;

import java.util.List;
import java.util.Map;

import com.altuure.yagdao.blog.model.Category;
import com.altuure.yagdao.*;
import org.springframework.transaction.annotation.Transactional;

import com.altuure.yagdao.blog.dao.BlogPropertyDAO;
import com.altuure.yagdao.blog.dao.CategoryDAO;
import com.altuure.yagdao.blog.dao.LinkDAO;
import com.altuure.yagdao.blog.dao.PostDAO;
import com.altuure.yagdao.blog.dao.TagDAO;
import com.altuure.yagdao.blog.model.Link;
import com.altuure.yagdao.blog.model.Post;
import com.altuure.yagdao.blog.model.Tag;
import com.altuure.yagdao.blog.service.BrowserService;

@Transactional(readOnly = true)
public class BrowserServiceImpl implements BrowserService {
    private BlogPropertyDAO blogDAO;
    private PostDAO postDAO;
    private TagDAO tagDAO;
    private LinkDAO linkDAO;
    private CategoryDAO categoryDAO;
    private int pageSize=10;


    public Map<String, String> getConf() {
        // TODO Auto-generated method stub
        return null;
    }


    public SearchResultList<Post> getPostList(int page, String tag, String category) {

        return postDAO.listTagAndCategory(tag, category, getLimit(page));
    }



    public SearchResultList<Post> listbyDate() {
        return postDAO.listbyDate();
    }


    public SearchResultList listByMonth(int year, int month, YPage yPage) {
        return postDAO.listByMonth(year, month, yPage);
    }


    public SearchResultList listTagAndCategory(String tag, String category, YPage yPage) {
        return postDAO.listTagAndCategory(tag, category, yPage);
    }


    public SearchResultList<Post> search(String keyword) {
        return postDAO.search(keyword, new YPage("publishDate",true));
    }


    public List popularTags() {

        return tagDAO.popularTags(new YLimit(0, 10));
    }

    public List<Link> getLinks() {
        return linkDAO.getAll();
    }

    public Post getPost(long id) {

        Post post = postDAO.get(id);

        return post;
    }

    public List<Tag> getTags(long postId) {

        List<Tag> tags = tagDAO.findByPostId(postId);

        return tags;
    }

    public List<Category> getCategories() {

        return categoryDAO.getAllByName();
    }

    public int getPageCount() {

        return (int)Math.ceil(postDAO.count()/pageSize);
    }

    public SearchResultList<Post> getPostListByDate(Integer year, Integer month, int page) {
         if(month==null)
             return postDAO.listByYear(year,getLimit(page));
        else
            return postDAO.listByMonth(year,month,getLimit(page));
    }

     private YPage getLimit(int page) {
        return new YPage(page * pageSize ,pageSize);
    }
    /**
     * @param blogDAO the blogDAO to set
     */
    public void setBlogDAO(BlogPropertyDAO blogDAO) {
        this.blogDAO = blogDAO;
    }

    /**
     * @param postDAO the postDAO to set
     */
    public void setPostDAO(PostDAO postDAO) {
        this.postDAO = postDAO;
    }

    /**
     * @param tagDAO the tagDAO to set
     */
    public void setTagDAO(TagDAO tagDAO) {
        this.tagDAO = tagDAO;
    }

    /**
     * @param linkDAO the linkDAO to set
     */
    public void setLinkDAO(LinkDAO linkDAO) {
        this.linkDAO = linkDAO;
    }

    /**
     * @param categoryDAO the categoryDAO to set
     */
    public void setCategoryDAO(CategoryDAO categoryDAO) {
        this.categoryDAO = categoryDAO;
    }


}
